package com.iflytek.universitymanagement.controller;

import com.iflytek.universitymanagement.bigmodel.OptionalAPI;
import com.iflytek.universitymanagement.domain.LearningPlan;
import com.iflytek.universitymanagement.service.LearningPlanService;
import com.iflytek.universitymanagement.service.StudentService;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LearningPlanController {

    @Autowired
    private LearningPlanService learningPlanService;

    @Autowired
    private StudentService studentService;

    // 生成学习计划
    @PostMapping("/generate-plan")
    public ResponseEntity<?> generatePlan(@RequestBody Map<String, Object> request) {
        String studentName = (String) request.get("studentName");
        String subject = (String) request.get("subject");
        Integer days = (Integer) request.get("days");  // 获取天数参数

        if (studentName == null || studentName.isEmpty()) {
            return ResponseEntity.badRequest().body("学生姓名不能为空");
        }
        if (subject == null || subject.isEmpty()) {
            return ResponseEntity.badRequest().body("科目不能为空");
        }
        if (days == null || days <= 0) {
            return ResponseEntity.badRequest().body("天数必须大于 0");
        }

        try {
            // 获取学生ID
            Integer studentId = studentService.getStudentIdByName(studentName);
            if (studentId == null) {
                return ResponseEntity.badRequest().body("学生未找到");
            }

            // 构造问题，根据天数动态生成
            String question = "给我关于" + subject + "设置一个" + days + "天计划表";

            // 调用AI接口
            OptionalAPI optionalAPI = new OptionalAPI();
            String plan = optionalAPI.askAi(question);

            // 将AI回答存入数据库
            LearningPlan learningPlan = new LearningPlan();
            learningPlan.setStudentId(studentId);
            learningPlan.setSubject(subject);
            learningPlan.setDays(days);
            learningPlan.setPlanContent(plan);  // 保存生成的计划内容

            learningPlanService.saveLearningPlan(learningPlan);

            // 返回生成的计划
            return ResponseEntity.ok(Collections.singletonMap("plan", plan));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("生成学习计划失败");
        }
    }

    // 查看学习计划（通过学生姓名和科目）
    @GetMapping("/learning-plan")
    public ResponseEntity<?> getLearningPlan(@RequestParam String studentName, @RequestParam String subject) {
        if (studentName == null || studentName.isEmpty()) {
            return ResponseEntity.badRequest().body("学生姓名不能为空");
        }
        if (subject == null || subject.isEmpty()) {
            return ResponseEntity.badRequest().body("科目不能为空");
        }

        try {
            // 获取学生ID
            Integer studentId = studentService.getStudentIdByName(studentName);
            if (studentId == null) {
                return ResponseEntity.badRequest().body("学生未找到");
            }

            // 查询指定科目的学习计划
            var learningPlans = learningPlanService.getLearningPlansByStudentIdAndSubject(studentId, subject);
            if (learningPlans != null && !learningPlans.isEmpty()) {
                return ResponseEntity.ok(learningPlans);
            } else {
                return ResponseEntity.status(404).body("未找到对应的学习计划");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("查询学习计划失败");
        }
    }

    // 删除学习计划（通过学生姓名和科目）
    @DeleteMapping("/learning-plan/delete")
    public ResponseEntity<?> deleteLearningPlan(@RequestParam String studentName, @RequestParam String subject) {
        if (studentName == null || studentName.isEmpty()) {
            return ResponseEntity.badRequest().body("学生姓名不能为空");
        }
        if (subject == null || subject.isEmpty()) {
            return ResponseEntity.badRequest().body("科目不能为空");
        }

        try {
            // 获取学生ID
            Integer studentId = studentService.getStudentIdByName(studentName);
            if (studentId == null) {
                return ResponseEntity.badRequest().body("学生未找到");
            }

            // 删除指定科目的学习计划
            boolean deleted = learningPlanService.deleteLearningPlansByStudentIdAndSubject(studentId, subject);
            if (deleted) {
                return ResponseEntity.ok("学习计划删除成功");
            } else {
                return ResponseEntity.status(404).body("未找到对应的学习计划");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("删除学习计划失败");
        }
    }
}
